const express = require('express');
const router = express.Router();
const { sequelize, User } = require('../../models');
const { Op } = require('sequelize');
const { NotFoundError } = require('../../utils/errors');
const { Success, failure } = require('../../utils/responses');



router.get('/sex',async function(req,res){
   try{
    const male = await User.count({where:{sex:0}});
    const female = await User.count({where:{sex:1}});
    const unknown = await User.count({where:{sex:2}});

    const data = [
        {value:male,name:'男性'},
        {value:female,name:'女性'},
        {value:unknown,name:'未选择'}
    ];
    Success(res,'查询性别成功',{data})

   } catch (error){
       failure(res,error);
   }
});
router.get('/user',async function(req,res){
   try{
       const [results] =await sequelize.query(
           "SELECT DATE_FORMAT(`createdAt`,'%Y-%m') AS `month`,\n" +
           "COUNT(*) AS `value`\n" +
           "FROM `users`\n" +
           "GROUP BY `month`\n" +
           "ORDER BY `month` ASC"
       );
       const data = {
           months:[],
           values:[]
       };
       results.forEach(
           item=>{
               data.months.push(item.month);
               data.values.push(item.value);
           }
       );
       Success(res,'查询每月用户数量成功',{data});
   } catch(error){
       failure(res,error);
   }
});
module.exports = router;